const Controller = require('egg').Controller;
const _ = require('underscore');
const moment = require('moment');

class PageController extends Controller {

async orgCreate(){

    let orgTree = await this.service.orgFsll.findTree(
        this.ctx.session.member, { mode: 'select' }
      );
  
  
      await this.ctx.render('orgCreate.njk', { orgTree });
  
    }

async orgUpdateList(){

    let orgTree = await this.service.orgFsll.findTree(
        this.ctx.session.member, { mode: 'list' }
      );
  
  
      await this.ctx.render('orgUpdateList.njk', { orgTree });
}

async orgUpdate(){

    let orgTree = await this.service.orgFsll.findTree(
        this.ctx.session.member, { mode: 'select' }
      );

    const orgId = this.ctx.request.query.orgId;
    const org = await this.app.mysql.get('org', { id: orgId });
    const parentOrg = await this.app.mysql.get('org', { id: org.pid });

    await this.ctx.render('orgUpdate.njk', {
      org,
      parentOrg,
      orgTree
    });
  }

  async memberCreate() {
    let orgTree = await this.service.orgFsll.findTree(
      this.ctx.session.member, { mode: 'select' }
    );

    await this.ctx.render('memberCreate.njk', { orgTree });
  }

  async memberUpdate() {
    const member = await this.app.mysql.get('member', { id: this.ctx.query.id });

    if (member.leaveAt == '0000-00-00' || member.leaveAt == null) {
      member.leaveAt = '';
    } else {
      member.leaveAt = moment(member.leaveAt).format('YYYY/MM/DD');
    }

    const orgTree = await this.ctx.service.orgFsll.findTree(this.ctx.session.member, { mode: 'select' })

    await this.ctx.render('memberUpdate.njk', { orgTree, member });
  }

  async memberUpdateList() {
    let memberTree = await this.ctx.service.memberFsll.findTree(this.ctx.session.member);

    await this.ctx.render('memberUpdateList.njk', { memberTree });
  }

  async memberUpdatePop() {

    const orgTree = await this.ctx.service.orgFsll.findTree(this.ctx.session.member, { mode: 'select' })
    const memberTree = await this.ctx.service.memberFsll.findTree(this.ctx.session.member, { mode: 'select' });

    await this.ctx.render('memberUpdatePop.njk', { memberTree, orgTree});
  }

  async userList() {
    let memberTree = await this.ctx.service.memberFsll.findTree(this.ctx.session.member);

    memberTree = memberTree.map(item => {
      if (item.auth) {
        const authArr = item.auth.split(',');
        item.title = [];

        if (authArr.indexOf('core') > -1) item.title.push('管理员');
        if (authArr.indexOf('task') > -1) item.title.push('老师');
        if (authArr.indexOf('task-common') > -1) item.title.push('学生');

        item.titleStr = item.title.join('，');
      }

      return item;

    });

    await this.ctx.render('userList.njk', { memberTree });
  }

  async userUpdate() {
    let user = await this.app.mysql.get('user', { memberId: this.ctx.query.id });

    if (user) {
      if (user.auth) {
        user.authArr = user.auth.split(',');

        if (user.authArr.indexOf('task') > -1)  user.task = "老师";
        if (user.authArr.indexOf('task-common') > -1) user.task = '学生';

      }

      let member = await this.app.mysql.get('member', { id: this.ctx.query.id });
      let taskAuth = ['老师', '学生'];

      await this.ctx.render('userUpdate.njk', { user, member, taskAuth });

    } else {
      await this.ctx.render('message.njk', {
        status: "warn",
        title: "错误",
        message: "该用户不存在",
        backTitle: '返回用户列表',
        backPage: "/page/userList"
      });
      return;
    }
  }

  async userCreate() {
    let user = await this.app.mysql.get('user', { memberId: this.ctx.query.id });

    let member = await this.app.mysql.get('member', { id: this.ctx.query.id });
    let taskAuth = ['老师', '学生'];

    await this.ctx.render('userCreate.njk', { user, member, taskAuth });

    if (user){
      await this.ctx.render('message.njk', {
        status: "warn",
        title: "错误",
        message: "该用户已存在",
        backTitle: '返回用户列表',
        backPage: "/page/userList"
      });
      return;
    }
  }

  async taskAssign() {
   
    const { user, member } = this.ctx.session;

    await this.ctx.render('taskAssign.njk', { user, member});

  }

  async memberProfileList() {
    let memberTree = await this.ctx.service.memberFsll.findTree(this.ctx.session.member);

    await this.ctx.render('memberProfileList.njk', { memberTree });
  }

   
  async taskCreate() {
    const { user, member } = this.ctx.session;
    const serveMemberTree = await this.ctx.service.task.serveMemberTree(this.ctx.session.member, { mode: 'select' });

    await this.ctx.render('taskCreate.njk', { user, member, serveMemberTree });
  }

  async taskReview() {
    const { user, member } = this.ctx.session;
    const serveMemberTree = await this.ctx.service.task.serveMemberTree(this.ctx.session.member, { mode: 'select' });

    await this.ctx.render('taskReview.njk', { user, member, serveMemberTree });
  }

  async taskReviewReview() {
    const { user, member } = this.ctx.session;
    const serveMemberTree = await this.ctx.service.task.serveMemberTree(this.ctx.session.member, { mode: 'select' });

    await this.ctx.render('taskReviewReview.njk', { user, member, serveMemberTree });
  }

  async taskMineList() {
    const { member } = this.ctx.session;

    let taskList = await this.app.mysql.query(`
    SELECT
      T.id,
      T.title,
      T.content,
      DATE_FORMAT(T.\`date\`,'%Y-%m-%d') AS date,
      T.assignment,
      T.category,
      T.serveMemberName,
      C.commenterNetName,
      C.commenterPosition,
      C.content AS \`comment\`,
      C.createAt AS commentCreateAt
    FROM
      task AS T
      LEFT JOIN \`comment\` AS C ON T.id = C.taskId
    WHERE
      T.memberId = ?
    ORDER BY
      T.date DESC, T.id DESC, C.createAt;
    `, [member.id]);

    await this.ctx.render('taskMineList.njk', {
      taskList,
      target: member.target
    });
  }
  
  async taskMemberList() {
    let memberTree = await this.ctx.service.task.findMemberTreeCount(this.ctx.session.member);

    await this.ctx.render('taskMemberList.njk', { memberTree });
  }

  async taskList() {
    const memberId = this.ctx.query.id;

    let { netName, target } = await this.app.mysql.get('member', { id: memberId });

    let taskList = await this.app.mysql.query(`
    SELECT
      T.id,
      T.title,
      T.content,
      T.memberId,
      DATE_FORMAT(T.\`date\`,'%Y-%m-%d') AS date,
      T.category,
      T.serveMemberName,
      T.assignment,
      C.commenterNetName,
      C.commenterPosition,
      C.content AS \`comment\`,
      C.createAt AS commentCreateAt
    FROM
      task AS T
      LEFT JOIN \`comment\` AS C ON T.id = C.taskId
    WHERE
      T.memberId = ?
    ORDER BY
      T.date DESC, T.id DESC, C.createAt;
    `, [memberId]);

    await this.ctx.render('taskList.njk', {
      netName,
      target,
      taskList
    });
  }


}

module.exports = PageController;